﻿CREATE TRIGGER [dbo].[tri_Loan_Borrower]
ON [dbo].[t_Loan_Borrower]
FOR INSERT
AS

UPDATE B1 SET LastName = B0.LastName
FROM t_Borrower B1
INNER JOIN inserted LB1 ON B1.ID = LB1.BorrowerID AND LB1.Pos = 1
INNER JOIN t_Loan_Borrower LB0 ON LB0.LoanID = LB1.LoanID AND LB0.Pos = 0
INNER JOIN t_Borrower B0 ON B0.ID = LB0.BorrowerID
WHERE ISNULL(LTRIM(RTRIM(B1.LastName)),'') = ''

IF @@ROWCOUNT > 0
UPDATE A1
SET Street = A0.Street, CityName = A0.CityName, StateCode = A0.StateCode, HouseHoldTypeID = A0.HouseHoldTypeID,
		HousePayment = A0.HousePayment, Years = A0.Years, Months = A0.Months, LandLord = A0.LandLord, LandLordPhone = A0.LandLordPhone
FROM t_Address A1
INNER JOIN inserted LB1 ON A1.BorrowerID = LB1.BorrowerID AND A1.Pos = 0 AND LB1.Pos = 1
INNER JOIN t_Loan_Borrower LB0 ON LB0.LoanID = LB1.LoanID AND LB0.Pos = 0
INNER JOIN t_Address A0 ON A0.BorrowerID = LB0.BorrowerID AND A0.Pos = 0
